<?php

namespace App\Modules\OfficialAccount\Models;

use DB;
use Illuminate\Database\Eloquent\Model;

class OfficialAccount extends Model
{
    protected $tables = 'official_accounts';

    protected $fillable = ['distribution_channel_id', 'nickname', 'name', 'head_img', 'appid', 'appsecret', 'verify_txt', 'alias', 'qrcode_url', 'principal_name', 'service_type_info', 'func_info', 'authorizer_refresh_token', 'is_auth', 'cancel_auth_time', 'official_account_type', 'verify_type_info', 'subscribe_top_num', 'subscribe_day_maximum', 'sort_no', 'is_enabled'];

    /**
     * 根据appid获取公众号
     */
    static function officialAccountByAppid($appid)
    {
        return self::where('appid', isset($appid) ? $appid : '')->first();
    }

    /**
     * 根据appid获取公众号
     */
    static function officialAccountArrByAppid($appid)
    {
        $official_account = self::where('appid', isset($appid) ? $appid : '')->first();
        if (!empty($official_account)) {
            return $official_account->toArray();
        } else {
            return null;
        }
    }

    /**
     * 根据appid和distribution_channel_id获取公众号
     */
    static function officialAccountByAppidAndChannelId($appid, $distribution_channel_id)
    {
        return self::where(['appid' => $appid, 'distribution_channel_id' => $distribution_channel_id])->first();
    }

    /**
     * 根据distribution_channel_id获取公众号
     */
    static function officialAccountByChannelId($distribution_channel_id)
    {
        return self::where(['distribution_channel_id' => $distribution_channel_id])->first();
    }

    /**
     * 获取所有可用已授权公众号
     */
    static function officialAuthAccounts($is_auth, $is_enabled)
    {
        return self::where(['is_auth' => isset($is_auth) ? $is_auth : '', 'is_enabled' => isset($is_enabled) ? $is_enabled : ''])->groupBy('appid')->get();
    }
}
